package reduce.fz.com.reduce.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.custom.DoorBtn;
import reduce.fz.com.reduce.custom.DoorBtns;
import reduce.fz.com.reduce.custom.LeftMenuItem;
import reduce.fz.com.reduce.custom.MyRadioButton;
import reduce.fz.com.reduce.custom.TabIndicator;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.display.Display;
import reduce.fz.com.reduce.display.IToolBarDisplay;
import reduce.fz.com.reduce.display.MainDisplay;
import reduce.fz.com.reduce.display.ToolBarDisplay;
import reduce.fz.com.reduce.event.DoorFragmentShowEvent;
import reduce.fz.com.reduce.fragment.FindFragment;
import reduce.fz.com.reduce.fragment.FindTeamFragment;
import reduce.fz.com.reduce.fragment.MessageFragment;
import reduce.fz.com.reduce.fragment.MyTeamFragment;
import reduce.fz.com.reduce.fragment.SelfCenterFragment;
import reduce.fz.com.reduce.util.ExitApp;

/**
 * 左边抽屉式主页面(首页)
 */
public class DoorActivity extends BaseToolBarWithTabActivity implements View.OnClickListener {

    private Display display = new MainDisplay(DoorActivity.this, R.id.main_content);
    private IToolBarDisplay toolBarDisplay = new ToolBarDisplay(DoorActivity.this);
    private Fragment mCurrentFragment;
    private FragmentManager mFragmentManager;


    //左侧菜单组件
    private LeftMenuItem menuMyteam;
    private LeftMenuItem menuTeamCategory;
    private LeftMenuItem menuTeamRecommend;
    private LeftMenuItem menuStoryRecommend;
    private LeftMenuItem menuTeamCreate;
    private LeftMenuItem menuMe;
    private RoundNetworkImageView mIvUserHeader;
    private TextView mTvNickName;
    private TextView mTvCollect;
    private TextView mTvMessage;
    private TextView mTvSetting;

    public void initWidget() {
        menuMyteam = (LeftMenuItem) findViewById(R.id.menu_myteam);
        menuTeamCategory = (LeftMenuItem) findViewById(R.id.menu_teamcategory);
        menuTeamRecommend = (LeftMenuItem) findViewById(R.id.menu_team_recommend);
        menuStoryRecommend = (LeftMenuItem) findViewById(R.id.menu_story_recommend);
        menuTeamCreate = (LeftMenuItem) findViewById(R.id.menu_team_create);
        menuMe = (LeftMenuItem) findViewById(R.id.menu_me);
        mIvUserHeader = (RoundNetworkImageView) findViewById(R.id.iv_user_header);
        mTvNickName = (TextView) findViewById(R.id.tv_user_nickname);
        mTvCollect = (TextView) findViewById(R.id.tv_collect);
        mTvMessage = (TextView) findViewById(R.id.tv_message);
        mTvSetting = (TextView) findViewById(R.id.tv_setting);

        menuMyteam.setOnClickListener(this);
        menuTeamCategory.setOnClickListener(this);
        menuTeamRecommend.setOnClickListener(this);
        menuStoryRecommend.setOnClickListener(this);
        menuTeamCreate.setOnClickListener(this);
        menuMe.setOnClickListener(this);
        mTvCollect.setOnClickListener(this);
        mTvMessage.setOnClickListener(this);
        mTvSetting.setOnClickListener(this);
    }


    /**
     * 显示fragment(通过attach和detach避免重复创建)
     *
     * @param fragment
     * @param cacheKey
     */
    public void showFragment(Fragment fragment, String cacheKey) {
        android.support.v4.app.FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        //先确认如果和当前的fragment相同，则不做处理
        if (mCurrentFragment != null && mCurrentFragment == fragment) return;
        //从缓存中获取fragment
        Fragment cacheFragment = getFragmentByContext(cacheKey);
        if (mCurrentFragment != null)
            fragmentTransaction.detach(mCurrentFragment);
        if (cacheFragment != null)
            fragmentTransaction.attach(cacheFragment);
        else {
            fragmentTransaction.add(R.id.container, fragment, cacheKey).commit();
        }
        mCurrentFragment = fragment;
    }

    public String getCacheKey(int position, String prefix) {
        return prefix + "_" + position;
    }

    public Fragment getFragmentByContext(String name) {
        Fragment fragment = mFragmentManager.findFragmentByTag(name);
        return fragment;
    }

    private Long exitTime = 0L;
    private Long gap = 2000L;
    private SlidingMenu sm;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu_myteam:
                break;
            case R.id.menu_teamcategory:
                break;
            case R.id.menu_team_recommend:
                break;
            case R.id.menu_team_create:
                break;
            case R.id.menu_story_recommend:
                break;
            case R.id.tv_collect:
                break;
            case R.id.tv_message:
                break;
            case R.id.tv_setting:
                break;
        }
    }

    @Override
    public void initListener() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //自定义左侧图标
        customIcon = R.drawable.icon_toolbar_selector;
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setTitle("坚持");
        setContentView(R.layout.activity_door);
        initWidget();

        //设置菜单页
        sm = new SlidingMenu(this);
        sm.setMode(SlidingMenu.LEFT);
        sm.setShadowWidth(5);
        sm.setBehindOffsetRes(R.dimen.abc_action_bar1);
        sm.setShadowDrawable(R.color.gray);
        sm.setFadeDegree(0.35f);
        //设置slding menu的几种手势模式
        //TOUCHMODE_FULLSCREEN 全屏模式，在content页面中，滑动，可以打开sliding menu
        //TOUCHMODE_MARGIN 边缘模式，在content页面中，如果想打开slding ,你需要在屏幕边缘滑动才可以打开slding menu
        //TOUCHMODE_NONE 自然是不能通过手势打开啦
        sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        sm.setMenu(R.layout.layout_left_menu);
        sm.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);

    }

    @Override
    public void pressBar() {
        sm.toggle(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                sm.toggle(true);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /*public void onEvent(DoorFragmentShowEvent<HashMap> event) {
        switch (event.fragmentEnum) {
            case FINDTEAMFRAGMENT:
                display.showFIndTeamFragment();
                break;
            case PINGLUNFRAGMENT:
                display.showPinglunFragment();
            case JIANZHENGFRAGMENT:
                display.showJianZhengFragment(null);
            case FENSIFRAGMENT:
                display.showFensiFragment(null);
            default:
                break;
        }
    }*/

    /**
     * 退出
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > gap) {
                Toast.makeText(getApplicationContext(), R.string.again_to_quit, Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                ExitApp.exit(this);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
